package com.kitchen.ui.dialog.decorate
{
	import com.kitchen.KitchenEvent;
	import com.kitchen.ui.dialog.com.CoinIcon;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	internal class DecoGridCell extends Sprite
	{
		public var cdata:Object;
		private var loader:Loader;
		private var bitmapData:BitmapData;
		
		public function DecoGridCell(slaveSkin:BitmapData, cdata:Object)
		{
			super();
			this.cdata = cdata;
			this.layout(slaveSkin);
			this.useHandCursor = true;
			this.buttonMode = true;
			this.addEventListener(MouseEvent.CLICK, clickHandler);
		}
		
		private function layout(slaveSkin:BitmapData):void{
			var rec:Shape = new Shape();
			rec.graphics.drawRect(0, 0, 90, 90);
			this.addChild(rec);
			var coinItem:CoinIcon = new CoinIcon(slaveSkin);
			var count:TextField = new TextField();
			count.x = 30;
			count.selectable = false;
			count.text = this.cdata.nPrice+'';
			count.setTextFormat(new TextFormat('黑体', 12, 0xff0000, true));
			coinItem.x = 0;
			count.y =77;
			coinItem.y = 75;
			this.addChild(count);
			this.addChild(coinItem);
			loader = new Loader();
			loader.load(new URLRequest(this.cdata.sUrl));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedHander);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHander);
		}
		
		private function loadedHander(event:Event):void{
			this.bitmapData = new BitmapData(70, 70);
			this.bitmapData.copyPixels(Bitmap(loader.content).bitmapData, new Rectangle(0, 0, 70, 70), new Point(0, 0));
			var bmp:Bitmap = new Bitmap(this.bitmapData);
			this.addChild(bmp);
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadedHander);
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, errorHander);
			loader.unload();
			loader = null;
		}
		
		private function errorHander(ioe:IOErrorEvent):void{
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadedHander);
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, errorHander);
			loader = null;
		}
			
		private function clickHandler(event:MouseEvent):void{
			this.dispatchEvent(new Event(KitchenEvent.DECO_TEST));
		}
	}
}